package com.kool.kadmin.client;

import com.alibaba.fastjson.JSON;
import com.kool.kadmin.client.request.GetClientTopDailyReportReq;
import com.kool.kadmin.client.request.GetGlobalDailyReportReq;
import com.kool.kadmin.client.request.GetTopicTopDailyReportReq;
import com.kool.kadmin.client.request.GetUserTopDailyReportReq;
import com.kool.kadmin.client.vo.ClientTopReport;
import com.kool.kadmin.client.vo.DailyReportVo;
import com.kool.kadmin.client.vo.TopicTopReport;
import com.kool.kadmin.client.vo.UserTopReport;
import com.kool.kadmin.common.constant.ErrorCode;
import com.kool.kadmin.common.exception.AppException;
import com.kool.kadmin.common.util.HttpUtil;
import com.kool.kadmin.common.util.ResultUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.List;

/**
 * @author luyu
 * @date 2021-04-20 09:20
 */
@Component
@Slf4j
public class KreportClient {
    @Value("${kreport.url:null}")
    private String urlPrefix;

    /**
     * 查询平台日报表
     *
     * @param request
     * @return
     */
    public List<DailyReportVo> getGlobalDailyReport(GetGlobalDailyReportReq request) {
        String url = urlPrefix + "/kreport/getGlobalDailyReport";
        try {
            String response = HttpUtil.doPost(url, JSON.toJSONString(request), null, 5000, 5000);
            String data = ResultUtil.unpackData(response);
            return JSON.parseArray(data, DailyReportVo.class);
        } catch (IOException e) {
            log.error(e.getMessage(), e);
            throw new AppException(ErrorCode.CLIENT_ERROR, "kreport", e.getMessage());
        }
    }


    /**
     * 查询主题top日报表
     *
     * @param request
     * @return
     */
    public TopicTopReport getTopicTopDailyReport(GetTopicTopDailyReportReq request) {
        String url = urlPrefix + "/kreport/getTopicTopDailyReport";
        try {
            String response = HttpUtil.doPost(url, JSON.toJSONString(request), null, 5000, 5000);
            String data = ResultUtil.unpackData(response);
            return JSON.parseObject(data, TopicTopReport.class);
        } catch (IOException e) {
            log.error(e.getMessage(), e);
            throw new AppException(ErrorCode.CLIENT_ERROR, "kreport", e.getMessage());
        }
    }

    /**
     * 查询用户top日报表
     *
     * @param request
     * @return
     */
    public UserTopReport getUserTopDailyReport(GetUserTopDailyReportReq request) {
        String url = urlPrefix + "/kreport/getUserTopDailyReport";
        try {
            String response = HttpUtil.doPost(url, JSON.toJSONString(request), null, 5000, 5000);
            String data = ResultUtil.unpackData(response);
            return JSON.parseObject(data, UserTopReport.class);
        } catch (IOException e) {
            log.error(e.getMessage(), e);
            throw new AppException(ErrorCode.CLIENT_ERROR, "kreport", e.getMessage());
        }
    }

    /**
     * 查询客户端top日报表
     *
     * @param request
     * @return
     */
    public ClientTopReport getClientTopDailyReport(GetClientTopDailyReportReq request) {
        String url = urlPrefix + "/kreport/getClientTopDailyReport";
        try {
            String response = HttpUtil.doPost(url, JSON.toJSONString(request), null, 5000, 5000);
            String data = ResultUtil.unpackData(response);
            return JSON.parseObject(data, ClientTopReport.class);
        } catch (IOException e) {
            log.error(e.getMessage(), e);
            throw new AppException(ErrorCode.CLIENT_ERROR, "kreport", e.getMessage());
        }
    }
}
